/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.InOutCountBean;
import com.minierp.bean.InOutCountBeanComparator;
import com.minierp.bean.InOutDetailBean;
import com.minierp.bean.OutCostBean;
import com.minierp.bean.QueryParameterBean;
import com.minierp.bean.QueryResult;
import com.minierp.bean.StockBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.InTypeDao;
import com.minierp.dao.OutTypeDao;
import com.minierp.dao.ProductDao;
import com.minierp.dao.ProductTypeDao;
import com.minierp.dao.QueryDao;
import com.minierp.dao.StockBalanceDao;
import com.minierp.dao.StockDao;
import com.minierp.dao.StorehouseDao;
import com.minierp.dao.WorkCenterDao;
import com.minierp.err.PermissionDennyException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.EncStockMACtrHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.EnterpriseInfo;
import com.minierp.model.InOutHistory;
import com.minierp.model.Product;
import com.minierp.model.ProductType;
import com.minierp.model.Stock;
import com.minierp.model.StockBalance;
import com.minierp.model.Storehouse;
import com.minierp.view.StockInOutCountExcelView;
import com.minierp.view.StockInOutListExcelView;
import com.minierp.view.StockOutCostExcelView;
import com.minierp.view.StockSearchExcelView;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class StockMACtr extends MultiActionController {
	private ProductTypeDao productTypeDao;
	private InTypeDao inTypeDao;
	private OutTypeDao outTypeDao;
	private StorehouseDao storehouseDao;
	private StockDao stockDao;
	private StockBalanceDao stockBalanceDao;
	private ProductDao productDao;
	private WorkCenterDao workCenterDao;
	private QueryDao queryDao;
	private DataSource dataSource;
	private String searchView;
	private String searchPrintView;
	private String inOutListView;
	private String inOutListPrintView;
	private String inOutCountView;
	private String inOutCountPrintView;
	private String outCostView;
	private String outCostPrintView;
	private String inOutItemListForBusinessBillView;
	private String inOutItemListAloneView;
	private String inOutLogListView;

	public String getInOutLogListView() {
		return this.inOutLogListView;
	}

	public void setInOutLogListView(String inOutLogListView) {
		this.inOutLogListView = inOutLogListView;
	}

	public String getInOutItemListAloneView() {
		return this.inOutItemListAloneView;
	}

	public void setInOutItemListAloneView(String inOutItemListAloneView) {
		this.inOutItemListAloneView = inOutItemListAloneView;
	}

	public String getOutCostPrintView() {
		return this.outCostPrintView;
	}

	public void setOutCostPrintView(String outCostPrintView) {
		this.outCostPrintView = outCostPrintView;
	}

	public String getOutCostView() {
		return this.outCostView;
	}

	public void setOutCostView(String outCostView) {
		this.outCostView = outCostView;
	}

	public ModelAndView handleSearch(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (!EncCommonHelper.checkPrivilege(request, "STOC_SEAR")) {
			throw new PermissionDennyException("Permission Denny");
		} else {
			ModelAndView mv = new ModelAndView(this.searchView);
			String contentType = request.getParameter("content_type");
			if (contentType == null) {
				contentType = "";
			}

			List ptList = this.productTypeDao.getList(true);
			UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
			EnterpriseInfo ent = EncCommonHelper.getEnterpriseInfo(request);
			List stoList;
			if (ent.getStorTs().equals("Y")) {
				stoList = this.storehouseDao.getList(ub);
			} else {
				stoList = this.storehouseDao.getList(true);
			}

			long firstStoId = 0L;
			if (stoList.size() > 0) {
				Storehouse b = (Storehouse) stoList.get(0);
				firstStoId = b.getId();
			}

			QueryParameterBean b1 = EncStockMACtrHelper.handleSearch(request,
					firstStoId, this.dataSource);
			String[] countObjs = new String[]{"sum(s.cost)"};
			QueryResult qr = this.queryDao.doQuery(b1.getQParas(),
					b1.getOParas(), (String[]) null, b1.getShortNameMap(),
					b1.getRetObjs(), countObjs);
			if (contentType.equals("EXCEL")) {
				HashMap productTypeId = new HashMap();
				productTypeId.put("list",
						this.parseStock(qr.getList(), ptList, stoList));
				productTypeId.put("cost", qr.getExtCount()[0]);
				StockSearchExcelView stoId = new StockSearchExcelView();
				mv = new ModelAndView(stoId, productTypeId);
			} else if (contentType.equals("PRINT")) {
				mv = new ModelAndView(this.searchPrintView);
				mv.addObject("list",
						this.parseStock(qr.getList(), ptList, stoList));
				String productTypeId1 = request.getParameter("pro_type_id");
				String stoId1 = request.getParameter("sto_id");
				String stockAmount = request.getParameter("sto_amount");
				String stoName = "";
				String ptName = "";
				String amountName = "全部";
				if (stoId1 != null && !stoId1.equals("") && !stoId1.equals("0")) {
					stoName = EncCommonHelper.getSimpleObjectName(stoList,
							Long.parseLong(stoId1));
				} else {
					stoName = "全部仓库";
				}

				if (productTypeId1 != null && !productTypeId1.equals("")
						&& !productTypeId1.equals("0")) {
					ptName = EncCommonHelper.getSimpleObjectName(ptList,
							Long.parseLong(productTypeId1));
				} else {
					ptName = "全部类别";
				}

				if (stockAmount != null) {
					if (stockAmount.equals("LOWER_MIN")) {
						amountName = "低于安全库存";
					} else if (stockAmount.equals("UPPER_MAX")) {
						amountName = "高于最高库存";
					} else if (stockAmount.equals("UPPER_ZERO")) {
						amountName = "库存大于0";
					}
				}

				mv.addObject("stoName", stoName);
				mv.addObject("ptName", ptName);
				mv.addObject("amountName", amountName);
				mv.addObject("cost", qr.getExtCount()[0]);
				mv.addObject("searchTime", new Date());
			} else {
				mv.addAllObjects(b1.getParameters());
				mv.addObject("list",
						this.parseStock(qr.getList(), ptList, stoList));
				mv.addObject("cost", qr.getExtCount()[0]);
			}

			mv.addObject("productTypeList", ptList);
			mv.addObject("storehouseList", stoList);
			return mv;
		}
	}

	public ModelAndView handleInOutList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (!EncCommonHelper.checkPrivilege(request, "STOC_DETA")) {
			throw new PermissionDennyException("Permission Denny");
		} else {
			ModelAndView mv = new ModelAndView(this.inOutListView);
			String contentType = request.getParameter("content_type");
			if (contentType == null) {
				contentType = "";
			}

			List ptList = this.productTypeDao.getList(true);
			UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
			EnterpriseInfo ent = EncCommonHelper.getEnterpriseInfo(request);
			List stoList;
			if (ent.getStorTs().equals("Y")) {
				stoList = this.storehouseDao.getList(ub);
			} else {
				stoList = this.storehouseDao.getList(true);
			}

			String searchType = request.getParameter("search_type");
			String stoId = "0";
			String proTypeId = "0";
			String proName = "";
			String proCode = "";
			String bbCode = "";
			String type = "";
			String inTypeId = "0";
			String outTypeId = "0";
			String wcId = "0";
			String startDateStr = "";
			String endDateStr = "";
			mv.addObject("stoId", request.getParameter("sto_id"));
			mv.addObject("proTypeId", request.getParameter("pro_type_id"));
			mv.addObject("proName", request.getParameter("pro_name"));
			mv.addObject("proCode", request.getParameter("pro_code"));
			mv.addObject("bbCode", request.getParameter("bb_code"));
			mv.addObject("type", request.getParameter("type"));
			mv.addObject("inTypeId", request.getParameter("in_type_id"));
			mv.addObject("outTypeId", request.getParameter("out_type_id"));
			mv.addObject("wcId", request.getParameter("wc_id"));
			mv.addObject("startDate", request.getParameter("start_date"));
			mv.addObject("endDate", request.getParameter("end_date"));
			mv.addObject("searchType", searchType);
			SimpleDateFormat shortSf = new SimpleDateFormat("yyyy-MM");
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			Date now = new Date();
			String stoName;
			String ptName;
			String searchTime;
			if (searchType != null && !searchType.equals("")) {
				if (searchType.equals("2")) {
					stoId = request.getParameter("sto_id");
					startDateStr = request.getParameter("start_date");
					endDateStr = request.getParameter("end_date");
					mv.addObject("startDate", startDateStr);
					mv.addObject("endDate", endDateStr);
				} else {
					String arr = request.getParameter("cb_order_date");
					String inCost = request.getParameter("cb_pro_type");
					String outCost = request.getParameter("cb_pro_name");
					String bean = request.getParameter("cb_pro_code");
					stoName = request.getParameter("cb_bb_code");
					ptName = request.getParameter("cb_type");
					searchTime = request.getParameter("cb_in_type");
					String cbOutType = request.getParameter("cb_out_type");
					String cbWc = request.getParameter("cb_wc");
					stoId = request.getParameter("sto_id");
					if (arr != null && arr.equals("1")) {
						startDateStr = request.getParameter("start_date");
						endDateStr = request.getParameter("end_date");
						mv.addObject("cbOrderDate", arr);
					}

					if (inCost != null && inCost.equals("1")) {
						proTypeId = request.getParameter("pro_type_id");
						mv.addObject("cbProType", inCost);
					}

					if (outCost != null && outCost.equals("1")) {
						proName = request.getParameter("pro_name");
						mv.addObject("cbProName", outCost);
					}

					if (bean != null && bean.equals("1")) {
						proCode = request.getParameter("pro_code");
						mv.addObject("cbProCode", bean);
					}

					if (stoName != null && stoName.equals("1")) {
						bbCode = request.getParameter("bb_code");
						mv.addObject("cbBbCode", stoName);
					}

					if (ptName != null && ptName.equals("1")) {
						type = request.getParameter("type");
						mv.addObject("cbType", ptName);
					}

					if (searchTime != null && searchTime.equals("1")) {
						inTypeId = request.getParameter("in_type_id");
						mv.addObject("cbInType", searchTime);
					}

					if (cbOutType != null && cbOutType.equals("1")) {
						outTypeId = request.getParameter("out_type_id");
						mv.addObject("cbOutType", cbOutType);
					}

					if (cbWc != null && cbWc.equals("1")) {
						wcId = request.getParameter("wc_id");
						mv.addObject("cbWc", cbWc);
					}
				}
			} else {
				startDateStr = shortSf.format(now) + "-01";
				endDateStr = sf.format(now);
				mv.addObject("startDate", startDateStr);
				mv.addObject("endDate", endDateStr);
				mv.addObject("searchType", "2");
				if (stoList.size() > 0) {
					stoId = String.valueOf(((Storehouse) stoList.get(0))
							.getId());
					mv.addObject("stoId", stoId);
				}
			}

			if (EncCommonHelper.checkPurePrivilege(request, "NO_EARL_DATA")) {
				Date arg32 = EncCommonHelper.getEarlDataDate(request);
				if (startDateStr != null && !startDateStr.equals("")) {
					if (arg32.after(sf.parse(startDateStr))) {
						startDateStr = sf.format(arg32);
					}
				} else {
					startDateStr = sf.format(arg32);
				}
			}

			Object arg33;
			if (stoId.equals("0")) {
				arg33 = new ArrayList();
			} else {
				arg33 = this.getInOutList(stoId, startDateStr, endDateStr,
						proTypeId, "0", proCode, proName, bbCode, type,
						inTypeId, outTypeId, wcId, "BILL", "BILL_DATE");
			}

			BigDecimal arg34 = new BigDecimal(0);
			BigDecimal arg35 = new BigDecimal(0);

			for (int arg37 = 0; arg37 < ((List) arg33).size(); ++arg37) {
				InOutDetailBean arg36 = (InOutDetailBean) ((List) arg33)
						.get(arg37);
				arg34 = arg34.add(arg36.getInCost());
				arg35 = arg35.add(arg36.getOutCost());
			}

			if (contentType.equals("EXCEL")) {
				HashMap arg38 = new HashMap();
				ptName = "";
				if (startDateStr != null) {
					ptName = startDateStr;
				}

				if (endDateStr != null && !endDateStr.equals("")) {
					if (!ptName.equals("")) {
						ptName = ptName + " 至 ";
					}

					ptName = ptName + endDateStr;
				}

				arg38.put("list", arg33);
				arg38.put("inCost", arg34);
				arg38.put("outCost", arg35);
				arg38.put("searchTime", ptName);
				StockInOutListExcelView arg39 = new StockInOutListExcelView();
				mv = new ModelAndView(arg39, arg38);
				return mv;
			} else if (!contentType.equals("PRINT")) {
				mv.addObject("list", arg33);
				mv.addObject("inCost", arg34);
				mv.addObject("outCost", arg35);
				mv.addObject("productTypeList", ptList);
				mv.addObject("storehouseList", stoList);
				mv.addObject("wcList", this.workCenterDao.getList(true));
				mv.addObject("inTypeList", this.inTypeDao.getList(true));
				mv.addObject("outTypeList", this.outTypeDao.getList(true));
				return mv;
			} else {
				mv = new ModelAndView(this.inOutListPrintView);
				mv.addObject("list", arg33);
				mv.addObject("inCost", arg34);
				mv.addObject("outCost", arg35);
				stoName = EncCommonHelper.getSimpleObjectName(stoList,
						Long.parseLong(stoId));
				if (proTypeId != null && !proTypeId.equals("")
						&& !proTypeId.equals("0")) {
					ptName = EncCommonHelper.getSimpleObjectName(ptList,
							Long.parseLong(proTypeId));
				} else {
					ptName = "全部类别";
				}

				searchTime = "";
				if (startDateStr != null) {
					searchTime = startDateStr;
				}

				if (endDateStr != null && !endDateStr.equals("")) {
					if (!searchTime.equals("")) {
						searchTime = searchTime + " 至 ";
					}

					searchTime = searchTime + endDateStr;
				}

				mv.addObject("stoName", stoName);
				mv.addObject("ptName", ptName);
				mv.addObject("searchTime", searchTime);
				mv.addObject("proCode", proCode);
				mv.addObject("proName", proName);
				mv.addObject("type", type);
				return mv;
			}
		}
	}

	public ModelAndView handleInOutLogList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.inOutLogListView);
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		EnterpriseInfo ent = EncCommonHelper.getEnterpriseInfo(request);
		List stoList;
		if (ent.getStorTs().equals("Y")) {
			stoList = this.storehouseDao.getList(ub);
		} else {
			stoList = this.storehouseDao.getList(true);
		}

		String searchType = request.getParameter("search_type");
		String stoId = "0";
		String proId = "0";
		String proCode = "";
		String startDateStr = "";
		String endDateStr = "";
		mv.addObject("stoId", request.getParameter("sto_id"));
		mv.addObject("proId", request.getParameter("pro_id"));
		mv.addObject("proName", request.getParameter("pro_name"));
		mv.addObject("proCode", request.getParameter("pro_code"));
		mv.addObject("startDate", request.getParameter("start_date"));
		mv.addObject("endDate", request.getParameter("end_date"));
		mv.addObject("searchType", searchType);
		SimpleDateFormat shortSf = new SimpleDateFormat("yyyy-MM");
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Date now = new Date();
		stoId = request.getParameter("sto_id");
		if (searchType != null && !searchType.equals("")) {
			if (searchType.equals("2")) {
				startDateStr = request.getParameter("start_date");
				endDateStr = request.getParameter("end_date");
				mv.addObject("startDate", startDateStr);
				mv.addObject("endDate", endDateStr);
			} else {
				String arr = request.getParameter("cb_order_date");
				String cbProCode = request.getParameter("cb_pro_code");
				String cbProName = request.getParameter("cb_pro_name");
				if (arr != null && arr.equals("1")) {
					startDateStr = request.getParameter("start_date");
					endDateStr = request.getParameter("end_date");
					if (startDateStr == null || startDateStr.equals("")) {
						startDateStr = shortSf.format(sf.parse(endDateStr))
								+ "-01";
						mv.addObject("startDate", startDateStr);
					}

					mv.addObject("cbOrderDate", arr);
				}

				if (cbProName != null && cbProName.equals("1")) {
					proId = request.getParameter("pro_id");
					mv.addObject("cbProName", cbProName);
				}

				if (cbProCode != null && cbProCode.equals("1")) {
					proCode = request.getParameter("pro_code");
					mv.addObject("cbProCode", cbProCode);
				}
			}
		} else {
			startDateStr = shortSf.format(now) + "-01";
			endDateStr = sf.format(now);
			mv.addObject("startDate", startDateStr);
			mv.addObject("endDate", endDateStr);
			mv.addObject("searchType", "2");
		}

		List arr1 = this.getInOutList(stoId, startDateStr, endDateStr, "0",
				proId, proCode, "", "", "", "0", "0", "0", "LOG", "IO_TIME");
		mv.addObject("list", arr1);
		mv.addObject("storehouseList", stoList);
		return mv;
	}

	public ModelAndView handleInOutCount(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (!EncCommonHelper.checkPrivilege(request, "STOC_COUN")) {
			throw new PermissionDennyException("Permission Denny");
		} else {
			ModelAndView mv = new ModelAndView(this.inOutCountView);
			String contentType = request.getParameter("content_type");
			if (contentType == null) {
				contentType = "";
			}

			List ptList = this.productTypeDao.getList(true);
			UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
			EnterpriseInfo ent = EncCommonHelper.getEnterpriseInfo(request);
			List stoList;
			if (ent.getStorTs().equals("Y")) {
				stoList = this.storehouseDao.getList(ub);
			} else {
				stoList = this.storehouseDao.getList(true);
			}

			String searchType = request.getParameter("search_type");
			String stoId = "0";
			String proTypeId = "0";
			String proName = "";
			String proCode = "";
			String type = "";
			String inTypeId = "0";
			String outTypeId = "0";
			String wcId = "0";
			String startDateStr = "";
			String endDateStr = "";
			mv.addObject("stoId", request.getParameter("sto_id"));
			mv.addObject("proTypeId", request.getParameter("pro_type_id"));
			mv.addObject("proName", request.getParameter("pro_name"));
			mv.addObject("proCode", request.getParameter("pro_code"));
			mv.addObject("type", request.getParameter("type"));
			mv.addObject("inTypeId", request.getParameter("in_type_id"));
			mv.addObject("outTypeId", request.getParameter("out_type_id"));
			mv.addObject("wcId", request.getParameter("wc_id"));
			mv.addObject("startDate", request.getParameter("start_date"));
			mv.addObject("endDate", request.getParameter("end_date"));
			mv.addObject("searchType", searchType);
			SimpleDateFormat shortSf;
			String monthStartDateStr;
			if (searchType != null && !searchType.equals("")) {
				if (searchType.equals("2")) {
					stoId = request.getParameter("sto_id");
					startDateStr = request.getParameter("start_date");
					endDateStr = request.getParameter("end_date");
					mv.addObject("startDate", startDateStr);
					mv.addObject("endDate", endDateStr);
				} else {
					String arg41 = request.getParameter("cb_order_date");
					String arg43 = request.getParameter("cb_pro_type");
					monthStartDateStr = request.getParameter("cb_pro_name");
					String startDate = request.getParameter("cb_pro_code");
					String list = request.getParameter("cb_type");
					String preArr = request.getParameter("cb_in_type");
					String arr = request.getParameter("cb_out_type");
					String indexMap = request.getParameter("cb_wc");
					stoId = request.getParameter("sto_id");
					if (arg41 != null && arg41.equals("1")) {
						startDateStr = request.getParameter("start_date");
						endDateStr = request.getParameter("end_date");
						mv.addObject("cbOrderDate", arg41);
					}

					if (arg43 != null && arg43.equals("1")) {
						proTypeId = request.getParameter("pro_type_id");
						mv.addObject("cbProType", arg43);
					}

					if (monthStartDateStr != null
							&& monthStartDateStr.equals("1")) {
						proName = request.getParameter("pro_name");
						mv.addObject("cbProName", monthStartDateStr);
					}

					if (startDate != null && startDate.equals("1")) {
						proCode = request.getParameter("pro_code");
						mv.addObject("cbProCode", startDate);
					}

					if (list != null && list.equals("1")) {
						type = request.getParameter("type");
						mv.addObject("cbType", list);
					}

					if (preArr != null && preArr.equals("1")) {
						inTypeId = request.getParameter("in_type_id");
						mv.addObject("cbInType", preArr);
					}

					if (arr != null && arr.equals("1")) {
						outTypeId = request.getParameter("out_type_id");
						mv.addObject("cbOutType", arr);
					}

					if (indexMap != null && indexMap.equals("1")) {
						wcId = request.getParameter("wc_id");
						mv.addObject("cbWc", indexMap);
					}
				}
			} else {
				Date sf = new Date();
				shortSf = new SimpleDateFormat("yyyy-MM");
				startDateStr = shortSf.format(sf) + "-01";
				shortSf = new SimpleDateFormat("yyyy-MM-dd");
				endDateStr = shortSf.format(sf);
				mv.addObject("startDate", startDateStr);
				mv.addObject("endDate", endDateStr);
				mv.addObject("searchType", "2");
				if (stoList.size() > 0) {
					stoId = String.valueOf(((Storehouse) stoList.get(0))
							.getId());
					mv.addObject("stoId", stoId);
				}
			}

			SimpleDateFormat arg42 = new SimpleDateFormat("yyyy-MM-dd");
			shortSf = new SimpleDateFormat("yyyy-MM");
			monthStartDateStr = null;
			Date arg44 = null;
			if (startDateStr != null && !startDateStr.equals("")) {
				arg44 = arg42.parse(startDateStr);
				monthStartDateStr = shortSf.format(arg44) + "-01";
			}

			ArrayList arg45 = new ArrayList();
			ArrayList arg46 = new ArrayList();
			if (!stoId.equals("0")) {
				List arg47 = this.getInOutList(stoId, monthStartDateStr,
						endDateStr, proTypeId, "0", proCode, proName, "", type,
						inTypeId, outTypeId, wcId, "BILL", "PRODUCT_CODE");

				for (int inCost = 0; inCost < arg47.size(); ++inCost) {
					InOutDetailBean arg49 = (InOutDetailBean) arg47.get(inCost);
					if (arg44 != null && arg44.after(arg49.getBillDate())) {
						arg46.add(arg49);
					} else {
						arg45.add(arg49);
					}
				}
			}

			ArrayList arg48 = new ArrayList();
			HashMap arg50 = new HashMap();
			BigDecimal arg51 = new BigDecimal(0);
			BigDecimal outCost = new BigDecimal(0);

			InOutCountBean bean;
			for (int balaList = 0; balaList < arg45.size(); ++balaList) {
				InOutDetailBean b = (InOutDetailBean) arg45.get(balaList);
				arg51 = arg51.add(b.getInCost());
				outCost = outCost.add(b.getOutCost());
				String preBalList = String.valueOf(b.getProductId());
				int index;
				if (arg50.containsKey(preBalList)) {
					index = Integer.parseInt(((String) arg50.get(preBalList))
							.toString());
					bean = (InOutCountBean) arg48.get(index);
					bean.setInAmount(bean.getInAmount().add(b.getInAmount()));
					bean.setInCost(bean.getInCost().add(b.getInCost()));
					bean.setOutAmount(bean.getOutAmount().add(b.getOutAmount()));
					bean.setOutCost(bean.getOutCost().add(b.getOutCost()));
				} else {
					bean = new InOutCountBean();
					bean.setProductCode(b.getProductCode());
					bean.setProductName(b.getProductName());
					bean.setProductUnit(b.getProductUnit());
					bean.setProductId(b.getProductId());
					bean.setProductTypeId(b.getProductTypeId());
					bean.setInAmount(b.getInAmount());
					bean.setInCost(b.getInCost());
					bean.setOutAmount(b.getOutAmount());
					bean.setOutCost(b.getOutCost());
					arg48.add(bean);
					index = arg48.size() - 1;
					arg50.put(preBalList, String.valueOf(index));
				}
			}

			List arg52 = null;
			List arg53 = null;
			if (arg44 != null) {
				arg52 = this.stockBalanceDao.getList(Long.parseLong(stoId),
						arg42.parse(monthStartDateStr));
				arg53 = this.stockBalanceDao.getPreviousList(
						Long.parseLong(stoId), arg44);
			}

			BigDecimal zero = new BigDecimal(0);

			for (int productList = 0; productList < arg48.size(); ++productList) {
				bean = (InOutCountBean) arg48.get(productList);
				this.setPreAndBal(bean, arg52, arg53, arg46, ptList);
			}

			List arg54 = this.productDao.getList(Long.parseLong(proTypeId),
					proCode, proName);
			ArrayList zeroArr = new ArrayList();

			String ptName;
			for (int objs = 0; objs < arg54.size(); ++objs) {
				Product stoName = (Product) arg54.get(objs);
				ptName = String.valueOf(stoName.getId());
				if (!arg50.containsKey(ptName)) {
					InOutCountBean searchTime = new InOutCountBean();
					searchTime.setProductCode(stoName.getProductCode());
					searchTime.setProductName(stoName.getProductName());
					searchTime.setProductUnit(stoName.getUnit());
					searchTime.setProductId(stoName.getId());
					searchTime.setProductTypeId(stoName.getProductTypeId());
					this.setPreAndBal(searchTime, arg52, arg53, arg46, ptList);
					if (searchTime.getPreAmount().compareTo(zero) != 0) {
						zeroArr.add(searchTime);
					}
				}
			}

			arg48.addAll(zeroArr);
			Object[] arg55 = arg48.toArray();
			Arrays.sort(arg55, new InOutCountBeanComparator());
			if (contentType.equals("EXCEL")) {
				HashMap arg57 = new HashMap();
				ptName = "";
				if (startDateStr != null) {
					ptName = startDateStr;
				}

				if (endDateStr != null && !endDateStr.equals("")) {
					if (!ptName.equals("")) {
						ptName = ptName + " 至 ";
					}

					ptName = ptName + endDateStr;
				}

				arg57.put("list", arg55);
				arg57.put("inCost", arg51);
				arg57.put("outCost", outCost);
				arg57.put("searchTime", ptName);
				StockInOutCountExcelView arg59 = new StockInOutCountExcelView();
				mv = new ModelAndView(arg59, arg57);
				return mv;
			} else if (!contentType.equals("PRINT")) {
				mv.addObject("list", arg55);
				mv.addObject("inCost", arg51);
				mv.addObject("outCost", outCost);
				mv.addObject("productTypeList", ptList);
				mv.addObject("storehouseList", stoList);
				mv.addObject("wcList", this.workCenterDao.getList(true));
				mv.addObject("inTypeList", this.inTypeDao.getList(true));
				mv.addObject("outTypeList", this.outTypeDao.getList(true));
				return mv;
			} else {
				mv = new ModelAndView(this.inOutCountPrintView);
				mv.addObject("list", arg55);
				mv.addObject("inCost", arg51);
				mv.addObject("outCost", outCost);
				String arg56 = EncCommonHelper.getSimpleObjectName(stoList,
						Long.parseLong(stoId));
				if (proTypeId != null && !proTypeId.equals("")
						&& !proTypeId.equals("0")) {
					ptName = EncCommonHelper.getSimpleObjectName(ptList,
							Long.parseLong(proTypeId));
				} else {
					ptName = "全部类别";
				}

				String arg58 = "";
				if (startDateStr != null) {
					arg58 = startDateStr;
				}

				if (endDateStr != null && !endDateStr.equals("")) {
					if (!arg58.equals("")) {
						arg58 = arg58 + " 至 ";
					}

					arg58 = arg58 + endDateStr;
				}

				mv.addObject("stoName", arg56);
				mv.addObject("ptName", ptName);
				mv.addObject("searchTime", arg58);
				mv.addObject("proCode", proCode);
				mv.addObject("proName", proName);
				return mv;
			}
		}
	}

	private void setPreAndBal(InOutCountBean bean, List balaList,
			List preBalList, List preArr, List ptList) {
		boolean balaFound = false;
		BigDecimal zero = new BigDecimal(0);
		BigDecimal preAmount = new BigDecimal(0);
		BigDecimal preCost = new BigDecimal(0);
		int preBean;
		StockBalance j;
		if (balaList != null) {
			for (preBean = 0; preBean < balaList.size(); ++preBean) {
				j = (StockBalance) balaList.get(preBean);
				if (j.getProductId() == bean.getProductId()) {
					preAmount = j.getAmount();
					preCost = j.getCost();
					balaFound = true;
					break;
				}
			}
		}

		if (!balaFound && preBalList != null) {
			for (preBean = 0; preBean < preBalList.size(); ++preBean) {
				j = (StockBalance) preBalList.get(preBean);
				if (j.getProductId() == bean.getProductId()) {
					preAmount = j.getAmount();
					preCost = j.getCost();
					break;
				}
			}
		}

		int arg13;
		for (arg13 = 0; arg13 < preArr.size(); ++arg13) {
			InOutDetailBean arg12 = (InOutDetailBean) preArr.get(arg13);
			if (arg12.getProductId() == bean.getProductId()) {
				preAmount = preAmount.add(arg12.getInAmount()).subtract(
						arg12.getOutAmount());
				preCost = preCost.add(arg12.getInCost()).subtract(
						arg12.getOutCost());
			}
		}

		bean.setPreAmount(preAmount);
		bean.setPreCost(preCost);
		bean.setBalAmount(bean.getPreAmount().add(bean.getInAmount())
				.subtract(bean.getOutAmount()));
		bean.setBalCost(bean.getPreCost().add(bean.getInCost())
				.subtract(bean.getOutCost()));
		if (bean.getPreAmount().compareTo(zero) != 0) {
			bean.setPreUnitPrice(bean.getPreCost().divide(bean.getPreAmount(),
					4, 4));
		}

		if (bean.getInAmount().compareTo(zero) != 0) {
			bean.setInUnitPrice(bean.getInCost().divide(bean.getInAmount(), 4,
					4));
		}

		if (bean.getOutAmount().compareTo(zero) != 0) {
			bean.setOutUnitPrice(bean.getOutCost().divide(bean.getOutAmount(),
					4, 4));
		}

		if (bean.getBalAmount().compareTo(zero) != 0) {
			bean.setBalUnitPrice(bean.getBalCost().divide(bean.getBalAmount(),
					4, 4));
		}

		if (bean.getProductTypeId() != 0L) {
			for (arg13 = 0; arg13 < ptList.size(); ++arg13) {
				ProductType pt = (ProductType) ptList.get(arg13);
				if (pt.getId() == bean.getProductTypeId()) {
					bean.setProductTypeName(pt.getTypeName());
					bean.setPtNameSpell(pt.getNameSpell());
					break;
				}
			}
		}

	}

	public ModelAndView handleOutCost(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.outCostView);
		String contentType = request.getParameter("content_type");
		if (contentType == null) {
			contentType = "";
		}

		List ptList = this.productTypeDao.getList(true);
		String bbCode = request.getParameter("bb_code");
		String ptIdStr = request.getParameter("pro_type_id");
		String productCode = request.getParameter("pro_code");
		String productName = request.getParameter("pro_name");
		if (bbCode != null && !bbCode.equals("")) {
			long ptId = 0L;
			if (ptIdStr != null && !ptIdStr.equals("")) {
				ptId = Long.parseLong(ptIdStr);
			}

			List list = this.stockDao.getInOutList(0L, (Date) null,
					(Date) null, ptId, 0L, productCode, productName, bbCode,
					"", 0L, 0L, 0L, "BILL", "");
			List inTypeList = this.inTypeDao.getList(false);
			List outTypeList = this.outTypeDao.getList(false);
			ArrayList arr = new ArrayList();
			BigDecimal totalCost = new BigDecimal(0);

			for (int map = 0; map < list.size(); ++map) {
				Object[] objs = (Object[]) list.get(map);
				InOutDetailBean ioBean = this.parseInOutDetailBean(objs);
				OutCostBean bean = new OutCostBean();
				bean.setBbCode(ioBean.getBusinessBillCode());
				bean.setBillCode(ioBean.getBillCode());
				bean.setBillDate(ioBean.getBillDate());
				bean.setBillType(ioBean.getBillType());
				bean.setOtherName(ioBean.getOtherName());
				bean.setOtherType(ioBean.getOtherType());
				bean.setProductCode(ioBean.getProductCode());
				bean.setProductName(ioBean.getProductName());
				bean.setProductUnit(ioBean.getProductUnit());
				if (ioBean.getBillType().equals("IN")) {
					bean.setAmount(ioBean.getInAmount());
					bean.setUnitPrice(ioBean.getInUnitPrice());
					bean.setCost(ioBean.getInCost());
					bean.setIoTypeName(EncCommonHelper.getSimpleObjectName(
							inTypeList, ioBean.getIoTypeId()));
					totalCost = totalCost.subtract(bean.getCost());
				} else {
					bean.setAmount(ioBean.getOutAmount());
					bean.setUnitPrice(ioBean.getOutUnitPrice());
					bean.setCost(ioBean.getOutCost());
					bean.setIoTypeName(EncCommonHelper.getSimpleObjectName(
							outTypeList, ioBean.getIoTypeId()));
					totalCost = totalCost.add(bean.getCost());
				}

				arr.add(bean);
			}

			if (contentType.equals("EXCEL")) {
				HashMap arg21 = new HashMap();
				arg21.put("list", arr);
				arg21.put("totalCost", totalCost.toString());
				StockOutCostExcelView v = new StockOutCostExcelView();
				mv = new ModelAndView(v, arg21);
			} else if (contentType.equals("PRINT")) {
				mv = new ModelAndView(this.outCostPrintView);
				mv.addObject("list", arr);
				mv.addObject("totalCost", totalCost.toString());
			} else {
				mv.addObject("bbCode", bbCode);
				mv.addObject("proTypeId", ptIdStr);
				mv.addObject("proCode", productCode);
				mv.addObject("proName", productName);
				mv.addObject("list", arr);
				mv.addObject("totalCost", totalCost.toString());
			}
		}

		mv.addObject("productTypeList", ptList);
		return mv;
	}

	private InOutDetailBean parseInOutDetailBean(Object[] objs)
			throws Exception {
		InOutDetailBean bean = new InOutDetailBean();
		InOutHistory ioh = (InOutHistory) objs[0];
		bean.setBillCode(ioh.getBillCode());
		bean.setBillDate(ioh.getBillDate());
		bean.setIoTime(ioh.getIoTime());
		bean.setOtherType(ioh.getRequType());
		bean.setOtherName(ioh.getRequName());
		bean.setProductCode((String) objs[1]);
		bean.setProductName((String) objs[2]);
		bean.setProductUnit((String) objs[3]);
		bean.setProductTypeId(Long.parseLong(objs[4].toString()));
		bean.setProductId(ioh.getProId());
		bean.setBillType(ioh.getBillType());
		bean.setIoType(ioh.getIoType());
		bean.setBusinessBillCode(ioh.getBbCode());
		bean.setIoTypeId(ioh.getTypeId());
		bean.setBillId(ioh.getBillId());
		bean.setMark(ioh.getMark());
		bean.setOperType(ioh.getOperType());
		if (ioh.getIoType().equals("IN")) {
			bean.setInAmount(ioh.getAmount());
			bean.setInUnitPrice(ioh.getUnitPrice());
			bean.setInCost(ioh.getCost());
		} else {
			bean.setOutAmount(ioh.getAmount());
			bean.setOutUnitPrice(ioh.getUnitPrice());
			bean.setOutCost(ioh.getCost());
		}

		bean.setBalAmount(ioh.getStorAmou());
		bean.setBalCost(ioh.getStorCost());
		bean.setBalUnitPrice(ioh.getStorUp());
		return bean;
	}

	private List getInOutList(String stoId, String startDateStr,
			String endDateStr, String proTypeId, String proId, String proCode,
			String proName, String bbCode, String type, String inTypeId,
			String outTypeId, String wcId, String listType, String orderCode)
			throws Exception {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate = null;
		if (startDateStr != null && !startDateStr.equals("")) {
			startDate = sf.parse(startDateStr);
		}

		Date endDate = null;
		if (endDateStr != null && !endDateStr.equals("")) {
			endDate = sf.parse(EncCommonHelper.getNextDateStr(endDateStr,
					"yyyy-MM-dd"));
		}

		List list = this.stockDao.getInOutList(Long.parseLong(stoId),
				startDate, endDate, Long.parseLong(proTypeId),
				Long.parseLong(proId), proCode, proName, bbCode, type,
				Long.parseLong(inTypeId), Long.parseLong(outTypeId),
				Long.parseLong(wcId), listType, orderCode);
		ArrayList arr = new ArrayList();

		for (int i = 0; i < list.size(); ++i) {
			arr.add(this.parseInOutDetailBean((Object[]) list.get(i)));
		}

		return arr;
	}

	private String getOtherName(Map<String, String> map, long otherId,
			String otherType) {
		String key = otherType + "_" + otherId;
		if (map.containsKey(key)) {
			return ((String) map.get(key)).toString();
		} else {
			String otherName = PageHelper.getRequName(otherId, otherType,
					this.stockDao);
			map.put(key, otherName);
			return otherName;
		}
	}

	private List<?> parseStock(List<?> list, List<?> productTypeList,
			List<?> stoList) {
		ArrayList arr = new ArrayList();

		for (int i = 0; i < list.size(); ++i) {
			StockBean b = new StockBean();
			Object[] objs = (Object[]) list.get(i);
			Stock s = (Stock) objs[0];
			Product p = (Product) objs[1];
			b.setStock(s);
			b.setProduct(p);
			b.setProductTypeName(EncCommonHelper.getSimpleObjectName(
					productTypeList, p.getProductTypeId()));
			b.setStoName(EncCommonHelper.getSimpleObjectName(stoList,
					s.getStorehouseId()));
			if (s.getAmount().compareTo(new BigDecimal(0)) != 0) {
				b.setUnitPrice(s.getCost().divide(s.getAmount(), 4, 4));
			}

			arr.add(b);
		}

		return arr;
	}

	public ModelAndView handleInOutItemListForBusinessBill(
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String bbCode = request.getParameter("business_bill_code");
		String otherType = request.getParameter("other_type");
		String otherId = request.getParameter("other_id");
		String type = request.getParameter("type");
		if (otherId == null || otherId.equals("")) {
			otherId = "0";
		}

		List list = this.stockDao.getInOutList(bbCode, otherType,
				Long.parseLong(otherId));
		HashMap nameMap = new HashMap();
		ArrayList arr = new ArrayList();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

		for (int mv = 0; mv < list.size(); ++mv) {
			Object[] objs = (Object[]) list.get(mv);
			InOutDetailBean bean = new InOutDetailBean();
			bean.setBillCode(objs[0].toString());
			bean.setBillDate(sf.parse(objs[1].toString()));
			bean.setOtherType(objs[2].toString());
			bean.setOtherName(this.getOtherName(nameMap,
					Long.parseLong(objs[3].toString()), bean.getOtherType()));
			if (objs[4] != null) {
				bean.setProductCode(objs[4].toString());
			}

			bean.setProductName(objs[5].toString());
			if (objs[6] != null) {
				bean.setProductUnit(objs[6].toString());
			}

			bean.setBillType(objs[8].toString());
			if (bean.getBillType().equals("IN")) {
				bean.setInAmount(new BigDecimal(objs[7].toString()));
				bean.setInCost(new BigDecimal(objs[9].toString()));
			} else {
				bean.setOutAmount(new BigDecimal(objs[7].toString()));
				bean.setOutCost(new BigDecimal(objs[9].toString()));
			}

			arr.add(bean);
		}

		ModelAndView arg13;
		if (type != null && type.equals("ALONE")) {
			arg13 = new ModelAndView(this.inOutItemListAloneView);
			arg13.addObject("mark", "业务单：" + bbCode);
		} else {
			arg13 = new ModelAndView(this.inOutItemListForBusinessBillView);
		}

		arg13.addObject("list", arr);
		arg13.addObject("showCost", request.getParameter("show_cost"));
		return arg13;
	}

	public DataSource getDataSource() {
		return this.dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public ProductTypeDao getProductTypeDao() {
		return this.productTypeDao;
	}

	public void setProductTypeDao(ProductTypeDao productTypeDao) {
		this.productTypeDao = productTypeDao;
	}

	public QueryDao getQueryDao() {
		return this.queryDao;
	}

	public void setQueryDao(QueryDao queryDao) {
		this.queryDao = queryDao;
	}

	public String getSearchPrintView() {
		return this.searchPrintView;
	}

	public void setSearchPrintView(String searchPrintView) {
		this.searchPrintView = searchPrintView;
	}

	public String getSearchView() {
		return this.searchView;
	}

	public void setSearchView(String searchView) {
		this.searchView = searchView;
	}

	public StorehouseDao getStorehouseDao() {
		return this.storehouseDao;
	}

	public void setStorehouseDao(StorehouseDao storehouseDao) {
		this.storehouseDao = storehouseDao;
	}

	public String getInOutListPrintView() {
		return this.inOutListPrintView;
	}

	public void setInOutListPrintView(String inOutListPrintView) {
		this.inOutListPrintView = inOutListPrintView;
	}

	public String getInOutListView() {
		return this.inOutListView;
	}

	public void setInOutListView(String inOutListView) {
		this.inOutListView = inOutListView;
	}

	public StockBalanceDao getStockBalanceDao() {
		return this.stockBalanceDao;
	}

	public void setStockBalanceDao(StockBalanceDao stockBalanceDao) {
		this.stockBalanceDao = stockBalanceDao;
	}

	public StockDao getStockDao() {
		return this.stockDao;
	}

	public void setStockDao(StockDao stockDao) {
		this.stockDao = stockDao;
	}

	public String getInOutCountPrintView() {
		return this.inOutCountPrintView;
	}

	public void setInOutCountPrintView(String inOutCountPrintView) {
		this.inOutCountPrintView = inOutCountPrintView;
	}

	public String getInOutCountView() {
		return this.inOutCountView;
	}

	public void setInOutCountView(String inOutCountView) {
		this.inOutCountView = inOutCountView;
	}

	public InTypeDao getInTypeDao() {
		return this.inTypeDao;
	}

	public void setInTypeDao(InTypeDao inTypeDao) {
		this.inTypeDao = inTypeDao;
	}

	public OutTypeDao getOutTypeDao() {
		return this.outTypeDao;
	}

	public void setOutTypeDao(OutTypeDao outTypeDao) {
		this.outTypeDao = outTypeDao;
	}

	public String getInOutItemListForBusinessBillView() {
		return this.inOutItemListForBusinessBillView;
	}

	public void setInOutItemListForBusinessBillView(
			String inOutItemListForBusinessBillView) {
		this.inOutItemListForBusinessBillView = inOutItemListForBusinessBillView;
	}

	public ProductDao getProductDao() {
		return this.productDao;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	public WorkCenterDao getWorkCenterDao() {
		return this.workCenterDao;
	}

	public void setWorkCenterDao(WorkCenterDao workCenterDao) {
		this.workCenterDao = workCenterDao;
	}
}